package luke4fun.wheresmymoney.fragments;

import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import java.util.Calendar;
import java.util.Date;

import luke4fun.wheresmymoney.R;
import luke4fun.wheresmymoney.service.async.ExportAsyncTask;
import luke4fun.wheresmymoney.util.DateUtil;
import luke4fun.wheresmymoney.util.FormatUtil;

public class ExportFragment extends Fragment {

	private Context context;
	private EditText exportDateFrom;
	private EditText exportDateTo;
	private Button exportBtn;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		context = getActivity();
		
		// init views
		View v = inflater.inflate(R.layout.fragment_export, container, false);
		exportDateFrom = (EditText) v.findViewById(R.id.export_date_from);
		exportDateTo   = (EditText) v.findViewById(R.id.export_date_to);
		exportBtn	   = (Button) v.findViewById(R.id.btn_export);
		
		setView();
		setListener();
		
		return v;
	}

    @Override
    public void onResume() {
        super.onResume();
        getActivity().setTitle(getString(R.string.title_export));
    }
	
	private void setView() {
		final Calendar c 	= Calendar.getInstance();
		int year			= c.get(Calendar.YEAR);
		int month			= c.get(Calendar.MONTH);
		c.clear();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month);
		
		DateUtil.getCurrentMonthInMillis();
		// set up datepicker dialog
		exportDateFrom.setText((month + 1) + "/" + 1 + "/" + year);
		exportDateTo.setText((month + 2) + "/" + 1 + "/" + year);
	}
	
	private void setListener() {
		final long timeInMillis = DateUtil.getCurrentMonthInMillis();
		exportDateFrom.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				DialogFragment dialog = DatePickerFragment.newInstance(
											v, 
											timeInMillis);
				dialog.show(getActivity().getSupportFragmentManager(), "datepicker");
			}
		});
		
		exportDateTo.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				DialogFragment dialog = DatePickerFragment.newInstance(
											v, 
											DateUtil.getNextMonthInMillis(timeInMillis));
				dialog.show(getActivity().getSupportFragmentManager(), "datepicker");
			}
		});
		
		exportBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				doExport();
			}
		});
	}
	
	private void doExport() {
		boolean mExternalStorageAvailable = false;
		boolean mExternalStorageWriteable = false;
		String state = Environment.getExternalStorageState();

		if (Environment.MEDIA_MOUNTED.equals(state)) {
		    // We can read and write the media
		    mExternalStorageAvailable = mExternalStorageWriteable = true;
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
		    // We can only read the media
		    mExternalStorageAvailable = true;
		    mExternalStorageWriteable = false;
		} else {
		    // Something else is wrong. 
			// It may be one of many other states, but all we need
		    // to know is we can neither read nor write
		    mExternalStorageAvailable = mExternalStorageWriteable = false;
		}
		
		if (mExternalStorageAvailable && mExternalStorageWriteable) {
			Date from = FormatUtil.parseDateFromMMddyyyy(exportDateFrom.getText().toString());
			Date to = FormatUtil.parseDateFromMMddyyyy(exportDateTo.getText().toString());
			
			new ExportAsyncTask(context).execute(from, to);
		}
	}
}
